﻿using Furion;
using Htx.Fur.Core.Options;
using Mapster;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Htx.Fur.Web.Core
{
    public class Startup : AppStartup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddJwt<JwtHandler>();

            services.AddCorsAccessor();

            //services.AddJwt();

            services.AddControllers()
                    .AddDynamicApiControllers()
                    .AddInjectWithUnifyResult()
                    .AddNewtonsoftJson(option =>    // 使用Newtonsoft.Json
                    {
                        option.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; // 忽略循环引用
                        option.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                        option.SerializerSettings.ContractResolver = new DefaultContractResolver();     // 原样输出属性名
                    })
                    .AddJsonOptions(option =>
                    {
                        option.JsonSerializerOptions.Converters.AddDateFormatString("yyyy-MM-dd HH:mm:ss", true /* DateTimeOffset类型 */);
                        option.JsonSerializerOptions.PropertyNamingPolicy = null;   // 原样输出属性名
                        //option.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;    // 处理循环引用问题
                        option.JsonSerializerOptions.IncludeFields = true;          // 包含成员字段序列化
                        option.JsonSerializerOptions.AllowTrailingCommas = true;    // 允许尾随括号
                        option.JsonSerializerOptions.ReadCommentHandling = JsonCommentHandling.Skip;    // 允许注释
                        option.JsonSerializerOptions.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping; // 处理乱码问题
                        option.JsonSerializerOptions.PropertyNameCaseInsensitive = true;    // 不区分大小写
                    })
                    ;

            //TypeAdapterConfig.GlobalSettings.Default.PreserveReference(true);

            // 配置选项依赖
            services.AddConfigurableOptions<AppInfoOptions>();

            // 添加简单的事件总线
            services.AddSimpleEventBus();

            // 分布式缓存
            services.AddStackExchangeRedisCache(option =>
            {
                option.Configuration = App.Configuration["Redis:Connection"];
                option.InstanceName = App.Configuration["Redis:Prefix"];
            });
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //app.UseHttpsRedirection();

            app.UseRouting();

            app.UseCorsAccessor();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseInject(string.Empty);

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}